/**
 * 
 */
package com.huniu.analysis.hive;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.huniu.analysis.colony.IScoutBeeAnalyse;
import com.huniu.analysis.colony.ScoutBeeAnalyseImpl;
import com.huniu.analysis.hive.assistant.INectarProducer;
import com.huniu.analysis.hive.assistant.NectarProducerImpl;
import com.huniu.links.exception.LinkProcessException;
import com.huniu.links.model.LinkXMLProperty;
import com.huniu.links.model.ModelXMLProperty;
import com.huniu.utility.DateUtil;

/**
 * @author zandy (chenheng)
 * 
 */
public class ScoutBeeAnalysePoolImpl implements IScoutBeeAnalysePool {

	private static final Logger logger = Logger
			.getLogger(ScoutBeeAnalysePoolImpl.class);
	private String qualityFolder ;
	private File[] dictionaryFiles;
	private List<File> linkFiles;
	private BlockingQueue<String[]> dicQueue = new LinkedBlockingQueue<String[]>();
	private boolean stopTag = false;
	
	
	public void setQualityFolder(String qualityFolder) {
		this.qualityFolder = qualityFolder;
	}

	public void setLinkFiles(List<File> linkFiles) {
		this.linkFiles = linkFiles;
	}

	public void setDictionaryFiles(File[] dictionaryFiles) {
		this.dictionaryFiles = dictionaryFiles;
	}

	private void startProduceDictionaries() {
		stopTag = false;
		logger.info("Start produce dictionaries ");
		logger.info("Generate a Nectar Producer with Blocking Queue : "
				+ dicQueue);
		INectarProducer nectarProducer = new NectarProducerImpl(dicQueue);
		logger.info("Set dictionary files :" + dictionaryFiles.length);
		nectarProducer.setDictionaryFiles(dictionaryFiles);
		logger.info("Generate a single thread executor");
		ExecutorService producerExecutor = Executors.newSingleThreadExecutor();
		logger.info("Start thread task");
		producerExecutor.execute(nectarProducer);
		producerExecutor.shutdown();
		while ( ! producerExecutor.isTerminated()) {
			continue ;
		}
		logger.info("All taskes have finished. Stop thread");
		stopTag = true;
	}

	@SuppressWarnings("unchecked")
	private List<String> loadLinks() throws DocumentException {
		logger.info("Load Links from link files ");
		List<String> linkList = new ArrayList<String>();
		for (File file : linkFiles) {
			SAXReader reader = new SAXReader();
			logger.info("Generate document from file :" + file.getName());
			Document document = reader.read(file);
			Element root = document.getRootElement();
			Iterator<Element> iterator = root
					.elementIterator(LinkXMLProperty.ELEMENT);
			while (iterator.hasNext()) {
				Element item = iterator.next();
				linkList.add(item.getText());
				logger.info("Get one link: " + item.getText());
			}
			generateAnalysedFile(file);
		}
		return linkList;
	}

	public void gatherNectar(List<String> linkList, String[] nectarArray,
			String qualityFolder, ExecutorService executor) {
		for (String link : linkList) {
			IScoutBeeAnalyse scoutAnalyse = new ScoutBeeAnalyseImpl();
			scoutAnalyse.setLink(link);
			scoutAnalyse.setNectarArray(nectarArray);
			scoutAnalyse.setQualityFolder(qualityFolder);
			executor.execute(scoutAnalyse);
		}

	}

	public void analyse() {
		if (dictionaryFiles != null) {
			try {
				startProduceDictionaries();
				List<String> linkList = loadLinks();
				String qualityFolder = generateQualityFolder();
				ExecutorService executor = Executors.newCachedThreadPool();
				while ( ! stopTag || ! dicQueue.isEmpty()) {
					String[] nectarArray = dicQueue.take();
					gatherNectar(linkList, nectarArray, qualityFolder, executor);
				} 
				executor.shutdown();
			} catch (DocumentException e) {

			} catch (InterruptedException e) {

			}
		}
	}
	
	private String generateQualityFolder() {
		GregorianCalendar calender = new GregorianCalendar();
		Date currentDate = calender.getTime();
		String childFolder = DateUtil.convertDateToString(currentDate,
				DateUtil.DATE_FORMAT_1);
		String folder = qualityFolder + File.separator + childFolder;
		File file = new File(folder);
		if (! file.exists()) {
			file.mkdirs();
		}
		return folder ;
	}
	
	private void generateAnalysedFile(File file) {
		String processedFileName = file.getAbsolutePath()
				+ ModelXMLProperty.ANALYSED_EXTENSION;
		File processedFile = new File(processedFileName);
		file.renameTo(processedFile);
	}
}
